package com.approval.approvalapp.index.dbsx.wsys;

import android.content.Context;
import android.os.Handler;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewParent;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.approval.approvalapp.R;
import com.approval.approvalapp.bean.WsysBean;
import com.approval.approvalapp.index.dbsx.wsys.fragment.WsYsDbFragment;
import com.approval.approvalapp.index.dbsx.wsys.fragment.WsYsYBFragment;
import com.approval.approvalapp.util.ErrorBeanUtil;
import com.approval.approvalapp.util.base.BaseActivity;
import com.approval.approvalapp.util.net.NetRequsetUtil;
import com.approval.approvalapp.util.net.interfacepac.NetDownResponse;
import com.approval.approvalapp.util.view.CustomTouchSwipeRefreshLayout;
import com.jcodecraeer.xrecyclerview.XRecyclerView;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.OnClick;

//网上预审
public class WsYsActivity extends BaseActivity {
    @Bind(R.id.back_iv)
    ImageView backIv;
    @Bind(R.id.titleTv1)
    TextView titleTv1;
    @Bind(R.id.xrv)
    XRecyclerView xrv;
    @Bind(R.id.swipeRefreshLayout)
    CustomTouchSwipeRefreshLayout swipeRefreshLayout;
    @Bind(R.id.empty_ll)
    LinearLayout empty_ll;
    private  int pagenum = 1;
    private List<WsysBean> wsysBeans;
    private WsysAdapter wsysAdapter;
    public static  WsYsActivity instance;

    @Override
    public int getLayoutId() {
        instance = this;
        return R.layout.activity_ws_ys;
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        instance = null;
    }

    @Override
    public void initView() {
        xrv.setPullRefreshEnabled(false);
        xrv.setLayoutManager(new LinearLayoutManager(this));
        wsysAdapter = new WsysAdapter();
        xrv.setAdapter(wsysAdapter);

        swipeRefreshLayout.setRefreshing(true);

        swipeRefreshLayout.postDelayed(new Runnable() {
            @Override
            public void run() {
                refreshData();
            }
        },500);

        swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                new Handler().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        xrv.setNoMore(false);
                        swipeRefreshLayout.setRefreshing(true);
                        refreshData();

                    }
                }, 500);
            }
        });

        xrv.setLoadingListener(new XRecyclerView.LoadingListener() {
            @Override
            public void onRefresh() {

            }

            @Override
            public void onLoadMore() {
                swipeRefreshLayout.setRefreshing(false);
                pagenum = pagenum + 1;
                addData(pagenum);
            }
        });



    }

    private void addData(int pagenum) {
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("page",pagenum);
        NetRequsetUtil.getInstance().netRequestPostJson("v1/user/wait_int_list",jsonObject.toString() , new NetDownResponse() {
            @Override
            public void success(String str) {
                List<WsysBean> mzcFgBeans = JSON.parseArray(str, WsysBean.class);
                if (mzcFgBeans == null || mzcFgBeans .size() == 0) {
                    xrv.setNoMore(true);
                }else {
                    for (int i = 0; i < mzcFgBeans.size(); i++) {
                        wsysBeans.add(mzcFgBeans.get(i));
                    }
                    wsysAdapter.setWsysBeanList(wsysBeans);
                    xrv.loadMoreComplete();
                }
            }

            @Override
            public void errowithresponse(String str) {
                ErrorBeanUtil.getErrorMessage(str);
                xrv.loadMoreComplete();
            }

            @Override
            public void erro() {
                ErrorBeanUtil.getErrorMessage(null);
                xrv.loadMoreComplete();
            }

            @Override
            public void finish() {
                swipeRefreshLayout.setRefreshing(false);
            }
        });
    }

    public void refreshData() {
        pagenum = 1;
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("page",1);
        NetRequsetUtil.getInstance().netRequestPostJson("v1/user/wait_int_list",jsonObject.toString() , new NetDownResponse() {
            @Override
            public void success(String str) {
                wsysBeans = JSON.parseArray(str, WsysBean.class);
                if (wsysBeans == null || wsysBeans.size() == 0){
                    empty_ll.setVisibility(View.VISIBLE);
                }else {
                    empty_ll.setVisibility(View.GONE);
                }
                wsysAdapter.setWsysBeanList(wsysBeans);
            }

            @Override
            public void errowithresponse(String str) {
                ErrorBeanUtil.getErrorMessage(str);
            }

            @Override
            public void erro() {

            }

            @Override
            public void finish() {
                swipeRefreshLayout.setRefreshing(false);
            }
        });
    }


    @OnClick(R.id.back_iv)
    public void onViewClicked() {
        finish();
    }

    @Override
    public void initData() {

    }

}
